﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AGVCallSystem
{
    /// <summary>
    /// SettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingWindow : Window
    {
        private bool amendFlag = false;
        CallDeviceBLL callDeviceBLL = new CallDeviceBLL();
        CallDeviceEntity callDeviceSelected;

        public SettingWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dg_CallDevice.ItemsSource = callDeviceBLL.GetCallDevices();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (amendFlag == true)
            {
                System.Windows.Forms.Application.Restart();
                System.Windows.Application.Current.Shutdown();
            }
        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tx_callNo.Text))
            {
                MessageBox.Show("请输入呼叫器编号！");
                return;
            }
            else if (!IsNum(tx_callNo.Text))
            {
                MessageBox.Show("请输入正确的数字！");
                return;
            }
            else if (string.IsNullOrEmpty(cb_Light.Text))
            {
                MessageBox.Show("请选择呼叫器灯！");
                return;
            }
            string callNum = tx_callNo.Text + "-" + cb_Light.Text;
            bool enableCall = (bool)ck_enable.IsChecked;
            CallDeviceEntity callDevice = new CallDeviceEntity();
            callDevice.CallDeviceNum = callNum;
            callDevice.CallState = false;
            callDevice.ExecuteState = false;
            callDevice.EnableCall = enableCall;
            if (callDeviceBLL.GetCallDeviceByNum(callNum) == null)
            {
                callDeviceBLL.AddCallDevice(callDevice);
                dg_CallDevice.ItemsSource = callDeviceBLL.GetCallDevices();
                amendFlag = true;
                MessageBox.Show("添加成功！");
            }
            else
            {
                MessageBox.Show("该呼叫编号已存在！");
                return;
            }
        }

        private void btn_Modify_Click(object sender, RoutedEventArgs e)
        {
            if (callDeviceSelected == null)
            {
                MessageBox.Show("请选择需要修改的行！");
                return;
            }
            else
            {
                string callNum = tx_callNo.Text + "-" + cb_Light.Text;
                callDeviceSelected.CallDeviceNum = callNum;
                callDeviceSelected.EnableCall = (bool)ck_enable.IsChecked;
                callDeviceBLL.EditCallDevice(callDeviceSelected);
                dg_CallDevice.ItemsSource = callDeviceBLL.GetCallDevices();
                amendFlag = true;
                callDeviceSelected = null;
                MessageBox.Show("修改成功！");
            }
        }

        private void btn_Delete_Click(object sender, RoutedEventArgs e)
        {
            if (callDeviceSelected == null)
            {
                MessageBox.Show("请选择需要删除的行！");
                return;
            }
            else
            {
                callDeviceBLL.DeleteCallDevice(callDeviceSelected);
                dg_CallDevice.ItemsSource = callDeviceBLL.GetCallDevices();
                amendFlag = true;
                callDeviceSelected = null;
                MessageBox.Show("删除成功！");
            }
        }

        private void dg_CallDevice_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                callDeviceSelected = e.AddedItems[0] as CallDeviceEntity;
                string[] strArray = callDeviceSelected.CallDeviceNum.Split('-');
                tx_callNo.Text = strArray[0];
                cb_Light.Text = strArray[1];
                ck_enable.IsChecked = callDeviceSelected.EnableCall;
            }
        }

        private bool IsNum(string numStr)
        {
            Regex regex = new Regex(@"^\+?[1-9][0-9]*$");
            return regex.IsMatch(numStr);
        }
    }
}
